﻿Imports System.Xml.Serialization
Imports System.Text.RegularExpressions

Public Class SmtpConfig
    Inherits DefaultConfig
    Private Shared mConfigInfo As New SmtpSetup
    Private Shared mConfigFilePath As String
    Private Shared mFileDateTime As DateTime


    REM 配置文件路径
    Public Overloads Shared ReadOnly Property ConfigFilePath() As String
        Get
            If String.IsNullOrEmpty(mConfigFilePath) OrElse Not IO.File.Exists(mConfigFilePath) Then

                Dim Path As String = AppSettings("ConfigPath")
                If Not Path.EndsWith("/") Then Path = String.Concat(Path, "/")
                mConfigFilePath = String.Format("{0}smtp.config", Path)

                If Not System.Web.HttpContext.Current Is Nothing Then
                    mConfigFilePath = System.Web.HttpContext.Current.Server.MapPath(mConfigFilePath)
                End If

            End If
            Return mConfigFilePath
        End Get
    End Property

    Public Shared Function Load() As SmtpSetup
        Return Load(True)
    End Function

    Public Shared Function Load(ByVal checktime As Boolean) As SmtpSetup
        mErrMessage = Nothing
        Try
            mConfigInfo = CType(LoadConfig(mFileDateTime, ConfigFilePath, mConfigInfo, checktime), SmtpSetup)

        Catch ex As Exception
            mErrMessage = ex.Message
            mConfigInfo = New SmtpSetup
        End Try
        Return mConfigInfo
    End Function

    Public Shared Sub Save()
        SaveConfig()
    End Sub

    Shared Sub New()

    End Sub
End Class

<Serializable()> <XmlRoot("SmtpSetup")> Public Class SmtpSetup
    Implements IConfigInfo

    Public CheckInteval As Integer
    Public SendInteval As Integer
    Public DefaultAccount As String
    Private CurrentName As String
    <XmlElement("Letter")> Public Letters() As Letter
    <XmlElement("Account")> Public Accounts() As Account

    Public Function Letter(ByVal Name As String) As Letter
        CurrentName = Name
        If Letters Is Nothing OrElse Letters.Length = 0 Then Return Nothing
        Return Array.Find(Letters, AddressOf FindWhere)
    End Function

    Public Function Find(ByVal Name As String) As Account
        CurrentName = Name
        If Accounts Is Nothing OrElse Accounts.Length = 0 Then Return Nothing
        Return Array.Find(Accounts, AddressOf FindWhere)
    End Function

    Private Function FindWhere(ByVal item As Letter) As Boolean
        Return item.Name = CurrentName
    End Function

    Private Function FindWhere(ByVal item As Account) As Boolean
        Return item.Name = CurrentName
    End Function

    Protected Overrides Sub Finalize()
        'Accounts = Nothing
        MyBase.Finalize()
    End Sub
End Class

<Serializable()> Public Class Letter
    Public ID As Integer
    Public Name As String
    Public LetterTitle As String
    Public LetterContent As String
End Class

<Serializable()> Public Class Account
    Public Name As String
    Public Server As String
    Public Port As Integer = 25
    Public Mail As String
    Public DisplayName As String
    Public User As String
    Public Passowrd As String
    Public EnableSsl As Boolean = False
End Class




